package com.dhcc.cdc.sys.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.cache.config.ICdcConfigCache;
import com.dhcc.cdc.sys.entity.ConfigurationEntity;
import com.dhcc.cdc.sys.service.IConfigurationService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.framework.util.AESUtils;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.codec.AesUtil;
import org.pentaho.di.core.encryption.Encr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 配置信息
 * 
 * @ClassName: ConfigurationController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-16 17:11:56
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/configuration")
public class ConfigurationController extends BaseController {

	private String PREFIX = "/backend/cdc/configuration/";
    private String productCode;
	@Autowired
	private IConfigurationService configurationService;

    /**
     * 跳转到配置信息列表首页
     */
    @RequestMapping("/index/{productCode}")
    @Permission("configuration:list")
    public String index(@PathVariable String productCode, Model model) {
        if("all".equals(productCode)) productCode=null;
        this.productCode = productCode;
        model.addAttribute("productCode",productCode);
        return PREFIX + "configuration.html";
    }
	
    /**
     * 跳转到新增配置信息
     */
    @RequestMapping("/to_add")
    @Permission("configuration:add")
    public String toAdd(Model model) {
        model.addAttribute("productCode",this.productCode);
        return PREFIX + "configuration_add.html";
    }
	
	/**
     * 跳转到修改配置信息、//
     */
    @RequestMapping("/to_update/{configurationId}")
    @Permission("configuration:update")
    public String toUpdate(@PathVariable Long configurationId, Model model) {
        ConfigurationEntity configuration = this.configurationService.selectById(configurationId);
        model.addAttribute("configuration",configuration);
        model.addAttribute("productCode",this.productCode);
        return PREFIX + "configuration_edit.html";
    }

    /**
     * 获取配置信息列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("configuration:list")
    public Object list(@RequestParam Map<String, Object> params) {
        if (this.productCode != null) {
            params.put("productCode", this.productCode);
        }
		Page<ConfigurationEntity> page = new PageFactory<ConfigurationEntity>().defaultPage();
        List<ConfigurationEntity> result = this.configurationService.page(page,params);
        page.setRecords((List<ConfigurationEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增配置信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("configuration:add")
    @BussinessLog(value = "新增配置信息")
    @CacheClear(ICdcConfigCache.CACHE_NAME)
    public Object add(ConfigurationEntity configuration) {
        if (CommonUtil.isOneEmpty(configuration)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        configuration.insert();
        return R.ok();
    }

    /**
     * 删除配置信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("configuration:delete")
    @BussinessLog(value = "删除配置信息")
    @CacheClear(ICdcConfigCache.CACHE_NAME)
    public Object delete(@RequestParam Long configurationId) {
	
        this.configurationService.deleteById(configurationId);

        return R.ok();
    }

    /**
     * 修改配置信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("configuration:update")
    @BussinessLog(value = "修改配置信息")
    @CacheClear(ICdcConfigCache.CACHE_NAME)
    public Object update(ConfigurationEntity configuration) {
        if (CommonUtil.isOneEmpty(configuration)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        ConfigurationEntity old = this.configurationService.selectById(configuration.getId());

        old.setCode(configuration.getCode());
        old.setDescription(configuration.getDescription());
        old.setValue(configuration.getValue());
        old.setNote(configuration.getNote());
        old.setProductId(configuration.getProductId());
        old.setHospitalId(configuration.getHospitalId());
        old.setIsActive(configuration.getIsActive());
        old.setUpdateDate(new Date());
        old.setUpdateUser(ShiroKit.getUser());
        old.updateAllColumnById();
        return R.ok();
    }

    @RequestMapping(value = "/encr/{value}")
    @ResponseBody
    public Object encr(@PathVariable("value") String value){
        return AesUtil.encode(value);
    }
}
